"""
富途账户扫描器
提供账户资金和持仓查询功能
"""
import logging
from typing import Dict, Any, List
from trading.futu_trader import FutuTrader


class BrokerAccountScanner:
    """富途券商账户扫描器"""
    
    def __init__(self, config_manager):
        self.config_manager = config_manager
        self.logger = logging.getLogger(__name__)
        
    def get_account_funds(self, account_info: Dict[str, Any], trading_password: str = "") -> Dict[str, Any]:
        """
        获取账户资金信息
        
        Args:
            account_info: 账户信息字典，包含账户ID、券商信息等
            trading_password: 交易密码
            
        Returns:
            Dict: 包含success、total_assets、currency等字段的结果
        """
        try:
            # 提取账户信息
            acc_id = account_info.get('账户ID') or account_info.get('acc_id')
            broker_info = account_info.get('券商') or account_info.get('broker')
            market_info = account_info.get('市场') or account_info.get('market')
            
            if not acc_id:
                return {
                    'success': False,
                    'status': 'error',
                    'message': '缺少账户ID',
                    'total_assets': 0.0,
                    'currency': 'USD'
                }
                
            # 创建FutuTrader实例
            futu_trader = FutuTrader(self.config_manager)
            
            # 获取资金信息
            funds_info = futu_trader.get_account_funds(str(acc_id))
            
            if funds_info and funds_info.get('total_assets', 0) > 0:
                return {
                    'success': True,
                    'status': 'success', 
                    'total_assets': funds_info.get('total_assets', 0.0),
                    'currency': funds_info.get('currency', 'USD'),
                    'power': funds_info.get('power', 0.0),
                    'max_power_short': funds_info.get('max_power_short', 0.0),
                    'net_cash_power': funds_info.get('net_cash_power', 0.0),
                    'message': '获取账户资金成功'
                }
            else:
                return {
                    'success': False,
                    'status': 'error',
                    'message': '无法获取账户资金信息',
                    'total_assets': 0.0,
                    'currency': 'USD'
                }
                
        except Exception as e:
            self.logger.error(f"获取账户资金失败: {e}")
            return {
                'success': False,
                'status': 'error',
                'message': f'获取账户资金失败: {str(e)}',
                'total_assets': 0.0,
                'currency': 'USD'
            }
    
    def get_account_positions(self, account_info: Dict[str, Any], trading_password: str = "") -> Dict[str, Any]:
        """
        获取账户持仓信息
        
        Args:
            account_info: 账户信息字典
            trading_password: 交易密码
            
        Returns:
            Dict: 包含status、positions等字段的结果
        """
        try:
            # 提取账户信息
            acc_id = account_info.get('账户ID') or account_info.get('acc_id')
            
            if not acc_id:
                return {
                    'status': 'error',
                    'message': '缺少账户ID',
                    'positions': []
                }
                
            # 创建FutuTrader实例
            futu_trader = FutuTrader(self.config_manager)
            
            # 这里返回一个空的持仓列表，因为具体的持仓查询功能可能在FutuTrader中有不同的实现
            # 如果需要具体的持仓信息，可以调用FutuTrader的相关方法
            return {
                'status': 'success',
                'message': '获取持仓信息成功',
                'positions': []  # 空列表，因为持仓信息通过其他方式获取
            }
                
        except Exception as e:
            self.logger.error(f"获取账户持仓失败: {e}")
            return {
                'status': 'error',
                'message': f'获取账户持仓失败: {str(e)}',
                'positions': []
            }
